package algorithm.leetcode;

import java.util.Stack;

/**
 * @program: util
 * @description: 有效的括号
 * 左括号入栈，右括号判断是否与栈顶匹配，匹配出栈
 * @author: zhouliangliang
 * @create: 2021-04-06 16:11
 **/
public class ValidParentheses {

    public boolean valid(String value) {
        Stack<Character> stack = new Stack();

        for(int i=0; i<value.length(); i++) {
            Character ch = value.charAt(i);
            if(ch.equals('(') || ch.equals('[') || ch.equals('{')) {
                stack.push(ch);
                continue;
            }

            if(ch.equals(')') && stack.peek().equals('(')) {
                stack.pop();
                continue;
            } else if(ch.equals(']') && stack.peek().equals('[')) {
                stack.pop();
                continue;
            } else if(ch.equals('}') && stack.peek().equals('{')) {
                stack.pop();
                continue;
            } else {
                break;
            }
        }
        boolean flag = stack.empty();

        if(!flag) {
            stack.stream().forEach(item->{
                System.out.print(item + ",");
            });
        }

        return flag;
    }

    public static void main(String[] args) {
        System.out.println(new ValidParentheses().valid("[]{{{}]}}"));
        System.out.println(new ValidParentheses().valid("[]{{[{}]}}"));
    }
}